package com.aft.terra.common.interceptor;

import org.apache.commons.lang3.StringUtils;

import com.aft.terra.app.Constants;
import com.aft.terra.common.utils.RedisKit;
import com.aft.terra.modules.game.model.User;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Cache;

public class OverAllParamsInterceptor implements Interceptor {


	/**
	 * 全局参数共享
	 * @param invocation 
	 * @see com.jfinal.aop.Interceptor#intercept(com.jfinal.aop.Invocation)
	 */
	public void intercept(Invocation invocation) {
		Controller action = invocation.getController();
		String userId = action.getCookie(Constants.SESSION_NAME);
		String uId = action.getPara("userId");
		if(StringUtils.isBlank(userId)){
			action.redirect("/?userId="+uId);
			return;
		}
		Cache cache = RedisKit.getCache();
		User wechat = cache.get(userId);
		if(wechat==null){
			action.redirect("/?userId="+uId);
			return;
		}
		
		action.setAttr("uId", uId);
		invocation.invoke();
	}
	
}
